<%@ page language="java" pageEncoding="us-ascii" contentType="text/html;charset=us-ascii" %>
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%--Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
	 
	This file is part of CDinFusion.
	
	CDinFusion is free software: you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public 
	License in version 3 (LGPL3) as published by the Free Software
	Foundation version 3 of the License.
	
	CDinFusion is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	Lesser General Public License for more details.
	
	You should have received a copy of the Lesser General Public
	License along with CDinFusion; see the file COPYING. If not,
	write to the Free Software Foundation, 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
	
	If you have any questions, send an email to: megx@mpi-bremen.de.
--%>
<%@ page import="org.springframework.webflow.execution.*"%>
<%@page import="java.net.HttpURLConnection"%>
<%@page import="java.util.Date"%>
<%@page import="org.cdinfusion.data.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.sql.Timestamp"%>

<%
System.out.println("<<<<<<<<<<GSC Selection Dispatcher>>>>>>>>>>");
HttpSession session = request.getSession();
Timestamp tstamp = new Timestamp(System.currentTimeMillis());
String sessionID = session.getId();
String destination = (String)request.getAttribute("destination");
String origin = (String) session.getAttribute("origin");
String context_exists = (String) session.getAttribute("context_exists");
String one_or_many = (String)session.getAttribute("one_or_many");
String initial = (String)session.getAttribute("initial");
if(destination.equals("one")){
	origin = "uploadfasta_one";
	session.setAttribute("origin", origin);
	List<String> allSequenceIDs = (List<String>) request.getAttribute("allSequenceIDs");
	session.setAttribute("allSequenceIDs", allSequenceIDs);
	%>
	<jsp:forward page="2a_GSC_SELECTED_1to1.jsp"/>
<%}

if(destination.equals("many")){
	origin = "uploadfasta_many";
	session.setAttribute("origin", origin);
	List<String> allSequenceIDs = (List<String>) request.getAttribute("allSequenceIDs");
	session.setAttribute("allSequenceIDs", allSequenceIDs);
	%>
	<jsp:forward page="2b_GSC_SELECTED_ntom.jsp"/>
<%}

if(destination.equals("csv")){
	CDElement cdelement = null;
	String study = "";
	String env = "";

	List<String> allSequenceIDs = (List<String>) session.getAttribute("allSequenceIDs");

	ArrayList<CDElement> cdeLIST = new ArrayList<CDElement>();
	List<String> usedSequenceIDs = new ArrayList<String>();
	List<String> currentlySelectedSIDs = new ArrayList<String>();
	
	String branch = (String) session.getAttribute("branch");
	
	if (branch.equals("csvupload")){
	cdeLIST = (ArrayList<CDElement>) request.getAttribute("cdeLIST");
    usedSequenceIDs = (List<String>) request.getAttribute("usedSequenceIDs");
    currentlySelectedSIDs = (List<String>) request.getAttribute("currentlySelectedSIDs");
	}
	
	session.setAttribute("currentlySelectedSIDs", currentlySelectedSIDs);
	
	if(origin.equals("2a")){
		origin = "uploadcsv";
		session.setAttribute("origin", origin);
		cdelement = cdeLIST.get(0);
		study = cdelement.getStudy().getClass().toString();
		study = study.replaceAll("class org.cdinfusion.data.studytype.", "").toLowerCase();
		env = cdelement.getEnv().getClass().toString();
		env = env.replaceAll("class org.cdinfusion.data.environment.", "").toLowerCase();
		
		session.setAttribute("cdeLIST", cdeLIST);
		%>
		<jsp:forward page="3a_CD_INPUT_1to1.jsp"/>
		<%
	}
	if(origin.equals("3b") | origin.equals("3b1")){
		origin = "uploadcsv";
		session.setAttribute("origin", origin);
		cdelement = cdeLIST.get(0);
		
		study = cdelement.getStudy().getClass().toString();
		study = study.replaceAll("class org.cdinfusion.data.studytype.", "").toLowerCase();
		env = cdelement.getEnv().getClass().toString();
		env = env.replaceAll("class org.cdinfusion.data.environment.", "").toLowerCase();
		
		session.setAttribute("cdeLIST", cdeLIST);
		// FIXME is this needed?
		session.setAttribute("seqIDs", CDElementHelper.getUsedSequenceIDs((List<String>)session.getAttribute("allSequenceIDs"), cdelement));
		session.setAttribute("usedSequenceIDs", usedSequenceIDs);
		if (usedSequenceIDs.size() == allSequenceIDs.size() & !one_or_many.equals("many")){
			%>
			<jsp:forward page="3b_CD_INPUT_1tom.jsp"/>
			<%
		}
		
		if (one_or_many.equals("many")){
			List<CDElement> tempcdelist = new ArrayList<CDElement>();

			if (initial.equals("false")){
				if((ArrayList<CDElement>) session.getAttribute("old_cdeLIST") != null){
					tempcdelist = (ArrayList<CDElement>) session.getAttribute("old_cdeLIST");
				}
				
				for (int i = 0; i < cdeLIST.size(); i++){
					tempcdelist.add(cdeLIST.get(i));
				}
				cdeLIST = (ArrayList<CDElement>)tempcdelist;
			}
			session.setAttribute("cdeLIST",cdeLIST);
			%>
			<jsp:forward page="3b2_CD_INPUT_ntom.jsp"/>
			<%
		}
		%>
		<jsp:forward page="3b2_CD_INPUT_ntom.jsp"/>
		<%
	}
}
%>